package com.example.bingobox.ui.todobox;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.Task;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class TaskSelectionAdapter extends RecyclerView.Adapter<TaskSelectionAdapter.ViewHolder> {
    private final List<Task> tasks;
    private final List<Task> selectedTasks = new ArrayList<>();

    public TaskSelectionAdapter(List<Task> tasks) {
        this.tasks = tasks;
    }

    public void preSelectTasks(Set<String> taskIds) {
        selectedTasks.clear();
        
        for (Task task : tasks) {
            if (taskIds.contains(task.getId())) {
                selectedTasks.add(task);
            }
        }
        
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_task_selection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Task task = tasks.get(position);
        holder.checkBox.setChecked(selectedTasks.contains(task));
        holder.nameText.setText(task.getName());
        holder.deadlineText.setText(formatDate(task.getDeadline()));

        View.OnClickListener clickListener = v -> {
            boolean isChecked = !holder.checkBox.isChecked();
            holder.checkBox.setChecked(isChecked);
            if (isChecked) {
                selectedTasks.add(task);
            } else {
                selectedTasks.remove(task);
            }
        };

        holder.itemView.setOnClickListener(clickListener);
        holder.checkBox.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    public List<Task> getSelectedTasks() {
        return new ArrayList<>(selectedTasks);
    }

    private String formatDate(long timestamp) {
        if (timestamp == 0) {
            return "永久";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
        return sdf.format(new Date(timestamp));
    }

    public void preSelectTask(Task task) {
        if (!selectedTasks.contains(task)) {
            selectedTasks.add(task);
            // 找到任务在列表中的位置并通知更新
            int position = tasks.indexOf(task);
            if (position != -1) {
                notifyItemChanged(position);
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox;
        TextView nameText;
        TextView deadlineText;

        ViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.checkbox_task);
            nameText = itemView.findViewById(R.id.text_task_name);
            deadlineText = itemView.findViewById(R.id.text_task_deadline);
        }
    }
} 